package sg.edu.nus.iss.vmcs.system;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.util.*;
import java.io.*;

import sg.edu.nus.iss.vmcs.store.*;

/**
 * This class represents the control object that implements the interface,
 * Property Loader, to provide the generic functionality required to initialize
 * the stores.
 * 
 * @version 1.0 09/12/2011
 * @author Team 7S - SE19
 */

public class FilePropertyLoader implements IStoreLoader {

	private static final String PROP_NUM_ITEMS = "NumOfItems";

	private Properties prop;
	private String fileName;

	/**
	 * This constructor creates an instance of FilePropertyLoader.
	 * 
	 * @param fileName
	 *            property file name
	 */
	public FilePropertyLoader(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * This operation reads the properties file into a hash table.
	 */
	public void initialize() throws IOException {
		prop = new Properties(System.getProperties());
		FileInputStream stream = new FileInputStream(fileName);
		prop.load(stream);
		stream.close();
	}

	/**
	 * This operation writes the properties from the hash table to the file.
	 * 
	 * @throws IOException
	 *             File Input/Output Exception
	 */
	public void save() throws IOException {
		FileOutputStream stream = new FileOutputStream(fileName);
		prop.store(stream, "");
		stream.close();
	}

	/**
	 * This operation returns the number of items (either Cash Store Items or
	 * Drink Store Items) stored in the hash table.
	 * 
	 * @return Number of Store Items
	 */
	public int getNumOfItems() {
		String nm = prop.getProperty(PROP_NUM_ITEMS);
		int nmi;
		nmi = Integer.parseInt(nm);
		return nmi;
	}

	/**
	 * This operation sets the number of items (either Cash Store Items or Drink
	 * Store Items) stored in the hash table.
	 * 
	 * @param vl
	 *            Number of Store Items
	 */
	public void setNumOfItems(int vl) {
		prop.setProperty(PROP_NUM_ITEMS, String.valueOf(vl));
	}

	/**
	 * This operation gets a value from the hash table.
	 * 
	 * @param key
	 *            Property Key to retrieve the value
	 * @return Gets the property value based on key
	 */
	public String getValue(String key) {
		return prop.getProperty(key);
	}

	/**
	 * This operation sets a value in the hash table.
	 * 
	 * @param key
	 *            Property Key to get the position in hash table
	 * @param value
	 *            Property value to set the located position in hash table
	 */
	public void setValue(String key, String value) {
		prop.setProperty(key, value);
	}

}